package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class Del implements IOperation{

    @Override
    public void work (BookList bookList) {
        System.out.println ("删除图书");
        int current=bookList.getUsedSize ();
        if(current==0){
            System.out.println ("书架为空");
            return;
        }

        System.out.println ("输入你要删除的图书");
        Scanner p=new Scanner (System.in);
        String name=p.nextLine ();



        //这里把flag的值设置为-1，既当作一个标志值，也当作返回值使用
        int flag=-1;
        for (int i = 0; i <current ; i++) {
            Book now=bookList.getBooks (i);
            if(now.getName ().equals (name)){
                flag=i;
                break;
            }
        }


        if(flag==-1) {
            System.out.println ("没有你要找的书");
            return;
        }

        for (int j = flag; j <current ; j++) {
            Book b1=bookList.getBooks (j+1);
            bookList.setBook (j,b1);
        }
        bookList.setUsedSize (current-1);
        System.out.println ("成功删除");
    }
}
